package org.abstractmeta.commandify.core.command.reflectify;

import org.abstractmeta.commandify.core.command.AbstractConstructorCommand;
import org.abstractmeta.reflectify.Reflectify;

/**
 * Reflectify constructor command
 */
public class ReflectifyConstructorCommand<T> extends AbstractConstructorCommand<T> {

    private final Reflectify.Provider<T> provider;

    public ReflectifyConstructorCommand(Reflectify<T> reflectify) {
        this(reflectify, new Class[]{});
    }

    public ReflectifyConstructorCommand(Reflectify<T> reflectify, Class[] argumentTypes, Object... arguments) {
        super(argumentTypes, arguments);
        this.provider = reflectify.getProvider(argumentTypes);
        for (int i = 0; i < arguments.length; i++) {
            Object argument = arguments[i];
            provider.getParameterSetter(i).set(argument);
        }
    }

    @Override
    public T execute() {
        return provider.get();
    }

}
